package com.ftd.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ftd.DBConnection.DBConnection;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.fluent.Request;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by apple on 16/8/1.
 */
@Controller
@RequestMapping("/hibook")
public class MarkViewDetailController {

    @RequestMapping(value = "getMarkDetail/{tag}.do", method = RequestMethod.GET)
    public ModelAndView getMarkDetail(@PathVariable String tag, ModelAndView modelAndView, HttpServletRequest req) throws IOException {

//        req.setAttribute("tag",tag);
        System.out.println("**************"+tag);

        ResponseHandler<String> myHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(
                    final HttpResponse response) throws IOException {
                return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
            }
        };

        String content = Request.Get("http://ac-EYUJxAnb.clouddn.com/3f4dab826d8249a7.MarkViewDetail").execute().handleResponse(myHandler);

        //content是json的一个对象,将其转换成String
        String jsonString = content;
        //讲json字符串转变成json对象
        JSONArray jsonArray = JSON.parseArray(jsonString);

        for (int i = 0; i < jsonArray.size() ; i++) {
            if(jsonArray.getJSONObject(i).getJSONObject("mTag").get("mTag").toString().equals(""+tag)){
                System.out.println("是");
                String comment = jsonArray.getJSONObject(i).getJSONObject("mTag").get("mComment").toString();
                String imageUrl = jsonArray.getJSONObject(i).get("mImageUrl").toString();
                String title = jsonArray.getJSONObject(i).get("mTitle").toString();

                modelAndView.addObject("comment",comment);
                modelAndView.addObject("imageUrl",imageUrl);
                modelAndView.addObject("title",title);

                modelAndView.setViewName("markDetailView");
                return modelAndView;
            }
        }
        return modelAndView;
    }

    @RequestMapping("star/{tag}")
    public String star(@PathVariable String tag){

        return "";
    }
}
